/**
 *   Copyright (c) fanguangping. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/
package org.jfunny.core.parser;

import java.io.PushbackReader;

import org.jfunny.core.ds.Symbol;

/**
 * 解析泛语言源代码词法的类
 * 
 * @author fanguangping
 *
 */
public class Scanner {
	public Scanner(PushbackReader reader) {
		this.reader = reader;
	}

	public boolean hasNext() throws Exception {
		while (true) {
			int c = read();

			switch (c) {
			case -1:
				return false;
				/*
				case ';':
					while ('\n' != c && -1 != c)
						c = read();
					break;
				*/
			case NOTE_L:
				while (NOTE_R != c && -1 != c)
					c = read();
				break;

			default:
				if (!Character.isWhitespace((char) c)) {
					reader.unread(c);
					return true;
				}
			}
		}
	}

	public Object next() throws Exception {
		while (true) {
			int c = read();

			switch (c) {
			case -1:
				throw new RuntimeException("Unexpected EOS");
			case '\\':
				c = read();
				return new Symbol(readSymbolNameEscaped(c));
			case '{':
				return Token.PAREN_L;
			case '}':
				return Token.PAREN_R;

				//分隔符作为单个符号
			case '、':
				return new Symbol("、");
			case '，':
				return new Symbol("，");
			case '；':
				return new Symbol("；");
				//字符串作为单个符号
			case '"':
				return new Symbol(readString(c));
			case '`':
				Symbol code = new Symbol(readCode(c));
				code.setCode(true);
				return code;
			default:
				return readSymbol(c);
			}
		}
	}

	private Object readSymbol(int c) throws Exception {
		String symbol = readSymbolName(c);
		return new Symbol(symbol);
	}

	private String readSymbolName(int c) throws Exception {
		buffer.setLength(0);

		while (!Character.isWhitespace((char) c) && '{' != c && '}' != c && NOTE_L != c && '、' != c && '，' != c
				&& '；' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		reader.unread(c);

		return buffer.toString();
	}

	private String readSymbolNameEscaped(int c) throws Exception {
		buffer.setLength(0);

		buffer.append((char) c);
		c = read();
		while (!Character.isWhitespace((char) c) && '{' != c && '}' != c && NOTE_L != c && '、' != c && '，' != c
				&& '；' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		reader.unread(c);

		return buffer.toString();
	}

	private String readString(int c) throws Exception {
		buffer.setLength(0);

		buffer.append((char) c);
		c = read();
		while ('"' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		buffer.append('"');

		return buffer.toString();
	}
	
	private String readCode(int c) throws Exception {
		buffer.setLength(0);

		c = read();
		while ('`' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		return buffer.toString();
	}

	private final int read() throws Exception {
		int c = reader.read();
		return 65535 == c ? -1 : c;
	}

	private PushbackReader reader;
	private StringBuffer buffer = new StringBuffer();

	private static final char NOTE_L = '（';
	private static final char NOTE_R = '）';
}
